package com.fan.teamer.process;

import java.util.Date;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

public class TeamUpdateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("TeamUpdateProcess");

	private Team origTeam;
	private String name;
	private String description;
	private String sport;
	private String icon;
	private String country;
	private String state;
	private String city;
	private String area;
	private String timezone;
	private String gender;
	private String ageProfile;
	private Date createdTime;
	private String attribute;

	private Member operator;

	private TeamService teamService;
	private MemberService memberService;
	private TeamMemberService teamMemberService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TeamUpdateProcess.logger = logger;
	}

	public Team getOrigTeam() {
		return origTeam;
	}

	public void setOrigTeam(Team origTeam) {
		this.origTeam = origTeam;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSport() {
		return sport;
	}

	public void setSport(String sport) {
		this.sport = sport;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getTimezone() {
		return timezone;
	}

	public void setTimezone(String timezone) {
		this.timezone = timezone;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAgeProfile() {
		return ageProfile;
	}

	public void setAgeProfile(String ageProfile) {
		this.ageProfile = ageProfile;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public int checkAutority() {

		if (teamMemberService.getTeamMember(origTeam, operator).getMemberRole()
				.equals(TeamerGlobalVariables.PARTICIPANT)) {
			setErrorMessage("Operator " + operator.getEmail()
					+ " cannot update Team: " + origTeam.getName() + " "
					+ origTeam.getCountry());
			return TeamerGlobalVariables.FAILED;
		} else {
			return TeamerGlobalVariables.SUCCESS;
		}
	}

	public int initCheck() {
		if (teamService == null || memberService == null) {
			setErrorMessage("Service cannot be null");
			return TeamerGlobalVariables.FAILED;
		}
		if (operator == null || origTeam == null) {
			setErrorMessage("Domain cannot be null");
			return TeamerGlobalVariables.FAILED;
		}

		if (!teamService.hasTeam(origTeam.getId())) {
			setErrorMessage("Team " + origTeam.getName()
					+ " doesn't exist in Country " + origTeam.getCountry()
					+ "!");
			return TeamerGlobalVariables.FAILED;
		}

		// updated team cannot be duplicated
		if (teamService.hasTeam(origTeam.getId())) {
			// name country already exist, but not origTeam
			if (!(origTeam.getName().equals(name) && origTeam.getCountry()
					.equals(country))) {
				setErrorMessage("Team " + name + " already exists!");
				return TeamerGlobalVariables.FAILED;
			}
		}

		if (!teamService.hasMember(origTeam, operator)) {
			setErrorMessage("Operator: " + operator.getEmail()
					+ " is not in Team: " + origTeam.getName() + " "
					+ origTeam.getCountry());
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;

	}

	@Override
	public Integer processImpl() {

		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		if (checkAutority() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		String origName = origTeam.getName();
		String origCountry = origTeam.getCountry();

		origTeam.setName(name);
		origTeam.setCountry(country);
		origTeam.setDescription(description);
		origTeam.setSport(sport);
		origTeam.setIcon(icon);
		origTeam.setState(state);
		origTeam.setCity(city);
		origTeam.setArea(area);
		origTeam.setTimezone(timezone);
		origTeam.setGender(gender);
		origTeam.setAgeProfile(ageProfile);
		origTeam.setAttribute(attribute);

		try {
			teamService.updateTeam(origTeam);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Team " + origName + " in country: " + origCountry
					+ " cannot be saved");
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;
	}

}
